package com.example.dotavideo.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dotavideo.R;
import com.example.dotavideo.VideoActivity;
import com.example.dotavideo.bean.VideoBean;
import com.nostra13.universalimageloader.core.ImageLoader;

public class VideoAdapter extends BaseAdapter{
  
 Context context;
 ArrayList<VideoBean> list;
 Handler handler;
 
 
  public VideoAdapter(Context context,ArrayList<VideoBean> list,Handler handler) {
    this.context = context;
    this.list = list;
    this.handler = handler;
  }
  
  @Override
  public int getCount() {
    return list != null ? list.size() : 0 ;
  }

  @Override
  public Object getItem(int arg0) {
    return null;
  }

  @Override
  public long getItemId(int arg0) {
    return 0;
  }

  @Override
  public View getView(int position, View view, ViewGroup group) {
    
    final Holder holder;
    // 得到View
    if (view == null) {
      holder = new Holder();
      LayoutInflater inflater = LayoutInflater.from(context);
      view = inflater.inflate(R.layout.video_row1, null);

      holder.ivIcon = (ImageView) view.findViewById(R.id.row_icon);
      holder.tvTitle = (TextView) view.findViewById(R.id.row_title);
      holder.tvDate = (TextView) view.findViewById(R.id.row_date);
      
      view.setTag(holder);
    } else {
      holder = (Holder) view.getTag();
    }
    
    final VideoBean bean = list.get(position);
    holder.tvTitle.setText(bean.getTitle());
    holder.tvDate.setText(bean.getDate());
    
    holder.ivIcon.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent(context,VideoActivity.class);
        intent.putExtra("video", bean);
        context.startActivity(intent);
      }
    });
    
    holder.ivIcon.setImageBitmap(null);
    String imageUrl = bean.getUrlImage();
  
    ImageLoader.getInstance().displayImage(imageUrl, holder.ivIcon);
    
    //加载更多了
    if (position == getCount() - 1) {
      if (handler!=null) {
        handler.sendEmptyMessage(1);
      }
    }
    
    return view;
  }

  
  class Holder {
    public ImageView ivIcon;
    public TextView tvTitle;
    public TextView tvDate;
  }

}
